<?php

namespace app\admin\model\bonus;

use think\Model;
use think\Env;

class Typebonus extends Model
{
    protected $connection = [
        // 数据库类型
        'type'        => 'mysql',
        // 数据库连接DSN配置
        'dsn'         => '',
        // 服务器地址
        'hostname'    => '47.93.51.61',
        // 数据库名
        'database'    => 'shop_fc62_com',
        // 数据库用户名
        'username'    => 'root',
        // 数据库密码
        'password'    => 'e7422254cd4628da',
        // 数据库连接端口
        'hostport'    => '',
        // 数据库连接参数
        'params'      => [],
        // 数据库编码默认采用utf8
        'charset'     => 'utf8',
        // 数据库表前缀
        'prefix'      => 'ecs_',
    ];
    // 表名
    protected $name = 'bonus_type';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;

    // 追加属性
    protected $append = [
        'order_type_text',
        'goods_type_text',
        'send_type_text',
        'day_time_text',
        'use_end_date_text',
        'send_text',
        'order_send_text'
    ];

    public function getOrderSendTextAttr($value, $data)
    {
        $value = $value ? $value : $data['type_id'];
        $userbonus = new Userbonus();
        return $userbonus->where('order_id','>',1)->where('bonus_type_id','=',$value)->count('bonus_type_id');
    }

    public function getSendTextAttr($value, $data)
    {
        $value = $value ? $value : $data['type_id'];
        $userbonus = new Userbonus();
        return $userbonus->where('bonus_type_id','=',$value)->count('bonus_type_id');
    }

    public function getOrderTypeList()
    {
        return ['0' => __('Order_type 0'),'1' => __('Order_type 1'),'2' => __('Order_type 2')];
    }     

    public function getGoodsTypeList()
    {
        return ['0' => __('Goods_type 0'),'1' => __('Goods_type 1')];
    }     

    public function getSendTypeList()
    {
        return [
            '0' => __('Send_type 0'),'1' => __('Send_type 1'),'2' => __('Send_type 2'),'3' => __('Send_type 3'),
            '6' => __('Send_type 6'),'7' => __('Send_type 7'),'10' => __('Send_type 10')
        ];
    }     


    public function getOrderTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['order_type'];
        $list = $this->getOrderTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGoodsTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['goods_type'];
        $list = $this->getGoodsTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSendTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['send_type'];
        $list = $this->getSendTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['day_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    protected function setDayTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUseEndDateAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
    public function getUseEndDateTextAttr($value, $data)
    {
        $value = $value ? $value : $data['use_end_date'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

}
